//
//  MCOpenHongBaoViewController.m
//  XDYL
//
//  Created by MC on 2018/3/23.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCOpenHongBaoViewController.h"
#import "OpenHongBaoView.h"
#import "MCCheckUserGrabMoneyModel.h"
#import "MCRedenvelopesUserOpenModel.h"
#import "MCHongBaoDataModel.h"

@interface MCOpenHongBaoViewController ()

@property (nonatomic,strong) OpenHongBaoView * openHongBaoView ;
@property (nonatomic,strong) MCCheckUserGrabMoneyModel * checkUserGrabMoneyModel;
@property (nonatomic,strong) MCRedenvelopesUserOpenModel * redenvelopesUserOpenModel;

@end


@implementation MCOpenHongBaoViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self  updatePreferredContentSizeWithTraitCollection:self.traitCollection];
    
    [self setProperty];
    
    [self  createUI];

//    [self relayOutHongBaoData:^(BOOL result, NSMutableArray *HongBaoArray) {
//    }];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}

-(void)setProperty{

    self.view.backgroundColor=[UIColor  clearColor];
    
}


-(void)createUI{
    __weak __typeof(self)wself = self;
        
    OpenHongBaoView * openHongBaoView = [[OpenHongBaoView alloc]init];
    [self.view addSubview:openHongBaoView];
    _openHongBaoView=openHongBaoView;
    [openHongBaoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    //退出
    _openHongBaoView.dismissblock = ^{
        
        [wself dismissViewControllerAnimated:NO completion:^{
            if (wself.dismissHongBaoVCBlock) {
                wself.dismissHongBaoVCBlock();
            }
        }];
        
    };
    _openHongBaoView.goToDetailHongBaoBlock = ^{
        [wself dismissViewControllerAnimated:NO completion:^{
            if (wself.goToDetailBlock) {
                wself.goToDetailBlock();
            }
        }];
    };
    
    _openHongBaoView.moveAnimationHongBaoBlock = ^{
        
        [wself openingHongBao];
        
    };
    
    MCHongBaoDataModel * dataSource = [MCHongBaoDataModel sharedMCHongBaoDataModel];
    if (dataSource.hongBaoMarray.count>0) {
        MCCheckUserGrabModel * model = [dataSource.hongBaoMarray objectAtIndex:dataSource.index];
        _openHongBaoView.dataSource = model;
    }
    

    
}
-(void)relayOutHongBaoData:(OpenHongBaoCompeletion)Compeletion{
    __weak __typeof(self)wself = self;
    
    NSDictionary * dic=@{};
    MCHongBaoDataModel * dataSource = [MCHongBaoDataModel sharedMCHongBaoDataModel];
    dataSource.index=0;
    MCCheckUserGrabMoneyModel * checkUserGrabMoneyModel = [[MCCheckUserGrabMoneyModel alloc]initWithDic:dic];
    [checkUserGrabMoneyModel refreashDataAndShow];
    self.checkUserGrabMoneyModel = checkUserGrabMoneyModel;
    
    checkUserGrabMoneyModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        Compeletion(NO,nil);
    };
    
    checkUserGrabMoneyModel.callBackSuccessBlock = ^(id manager) {
        NSArray * arr = manager;
        if (arr.count>0) {
            MCHongBaoDataModel * dataSource = [MCHongBaoDataModel sharedMCHongBaoDataModel];
            [dataSource setDataWithArr:arr];
            Compeletion(YES,dataSource.hongBaoMarray);
            if (wself.relayOutHongBaoDataBlock) {
                wself.relayOutHongBaoDataBlock();
            }
        }
    };
}


- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    [self updatePreferredContentSizeWithTraitCollection:newCollection];
    
}

#pragma mark-更新高度
- (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection{
    
    self.preferredContentSize = CGSizeMake(300, 150);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)delayDismissMethod{
    
    __weak __typeof(self)wself = self;
    
    [wself dismissViewControllerAnimated:NO completion:^{
        
        [SVProgressHUD showInfoWithStatus:@"暂无红包！"];
        
        if (wself.dismissHongBaoVCBlock) {
            wself.dismissHongBaoVCBlock();
        }
    }];
}

-(void)openingHongBao{
    MCHongBaoDataModel * dataSource = [MCHongBaoDataModel sharedMCHongBaoDataModel];
    if (!dataSource.index) {
        dataSource.index=0;
    }
    if (dataSource.hongBaoMarray.count<1) {
        [self relayOutHongBaoData:^(BOOL result, NSMutableArray *HongBaoArray) {
        }];
        
        [self performSelector:@selector(delayDismissMethod) withObject:nil/*可传任意类型参数*/ afterDelay:1.5];

        return;
    }
    MCCheckUserGrabModel * model = [dataSource.hongBaoMarray objectAtIndex:dataSource.index];
    NSDictionary * dic =@{@"Sysid":model.RedEnvelopId};
    
    MCRedenvelopesUserOpenModel * redenvelopesUserOpenModel = [[MCRedenvelopesUserOpenModel alloc]initWithDic:dic];
    
    [redenvelopesUserOpenModel refreashDataAndShow];
    self.redenvelopesUserOpenModel = redenvelopesUserOpenModel;
    
    redenvelopesUserOpenModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        
    };
    
    redenvelopesUserOpenModel.callBackSuccessBlock = ^(id manager) {
//        {
//            "ReceiveEnum": "UnExist",
//            "StandardAmount": 0,
//            "Amount": 0
//        }
        
        NSDictionary * dic = manager;
        [self dealData:dic];
        
    };
    
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [_openHongBaoView showResultWithType:HongBaoType_Success];
//        _openHongBaoView.successHongBaoView.dataSource=nil;
//
////        [_openHongBaoView showResultWithType:HongBaoType_Overdue];
////        _openHongBaoView.overdueHongBaoView.dataSource=nil;
////
////        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
////        _openHongBaoView.noMoneyHongBaoView.dataSource=nil;
//
//    });
    
}


-(void)dealData:(NSDictionary *)dic{
    
    NSString * ReceiveEnum = [NSString stringWithFormat:@"%@",dic[@"ReceiveEnum"]];
    NSString * FriendlyMessage = [NSString stringWithFormat:@"%@",dic[@"FriendlyMessage"]];
    
    MCHongBaoDataModel * dataSource = [MCHongBaoDataModel sharedMCHongBaoDataModel];
    NSInteger index = dataSource.index;
    BOOL isDelete = NO;
    
    
    
    //红包不存在
    if ([ReceiveEnum integerValue]==0) {
        
        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
//        _openHongBaoView.noMoneyHongBaoView.dataSource=@"手慢了，红包已被抢光！";
        _openHongBaoView.noMoneyHongBaoView.dataSource=FriendlyMessage;
        
        [dataSource HB_RemoveObjectAtIndex:dataSource.index];
        isDelete=YES;
        
        //成功
    }else if ([ReceiveEnum integerValue]==1){
        
        MCCheckUserGrabModel * model = [dataSource.hongBaoMarray objectAtIndex:dataSource.index];
        
        NSString * Amount = [NSString stringWithFormat:@"%@",GetTwoDigitRealSNum(dic[@"Amount"])];
        NSString * Remark = [NSString stringWithFormat:@"【%@】给你发了一个红包",model.SendName];
        [_openHongBaoView showResultWithType:HongBaoType_Success];
        [_openHongBaoView.successHongBaoView relayOutUIWithMoney:Amount andRemark:Remark];
        
        [dataSource HB_RemoveObjectAtIndex:dataSource.index];
        isDelete=YES;
        
        //红包被抢光
    }else if ([ReceiveEnum integerValue]==2){
        
        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
//        _openHongBaoView.1noMoneyHongBaoView.dataSource=@"手慢了，红包已被抢光！";
        _openHongBaoView.noMoneyHongBaoView.dataSource=FriendlyMessage;
        [dataSource HB_RemoveObjectAtIndex:dataSource.index];
        isDelete=YES;
        
        //过期
    }else if ([ReceiveEnum integerValue]==3){
        
        [_openHongBaoView showResultWithType:HongBaoType_Overdue];
        _openHongBaoView.overdueHongBaoView.dataSource=nil;
        [dataSource HB_RemoveObjectAtIndex:dataSource.index];
        isDelete=YES;
        //消费未达标
    }else if ([ReceiveEnum integerValue]==4){
        
        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
//        _openHongBaoView.noMoneyHongBaoView.dataSource=@"您未达标，无法领取红包！";
//        _openHongBaoView.noMoneyHongBaoView.standardAmount=[NSString stringWithFormat:@"（消费≥%@元才可领取喔）",dic[@"StandardAmount"]];
        _openHongBaoView.noMoneyHongBaoView.dataSource=FriendlyMessage;
        if ((dataSource.hongBaoMarray.count-1)==dataSource.index) {
        }else{
            dataSource.index=dataSource.index+1;
        }
        
        //充值未达标
    }else if ([ReceiveEnum integerValue]==5){
        
        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
//        _openHongBaoView.noMoneyHongBaoView.dataSource=@"您未达标，无法领取红包！";
//        _openHongBaoView.noMoneyHongBaoView.standardAmount=[NSString stringWithFormat:@"（充值≥%@元才可领取喔）",dic[@"StandardAmount"]];
        _openHongBaoView.noMoneyHongBaoView.dataSource=FriendlyMessage;
        //（消费≥x元 & 充值≥x元才可领取喔）
        if ((dataSource.hongBaoMarray.count-1)==dataSource.index) {
        }else{
            dataSource.index=dataSource.index+1;
        }
        
        //红包已抢过
    }else if ([ReceiveEnum integerValue]==6){
        
        [_openHongBaoView showResultWithType:HongBaoType_NoMoney];
//        _openHongBaoView.noMoneyHongBaoView.dataSource=@"红包已抢过";
        _openHongBaoView.noMoneyHongBaoView.dataSource= FriendlyMessage;
        [dataSource HB_RemoveObjectAtIndex:dataSource.index];
        isDelete=YES;
    }else{
        
    }
    
    /**到达最后一个  刷新**/
    if ((dataSource.hongBaoMarray.count-1)==index) {
            dataSource.index=0;
    }
    
    if ( isDelete && (dataSource.hongBaoMarray.count>0) ) {
        
    }else{
        if ((dataSource.hongBaoMarray.count-1)==index) {
            [self relayOutHongBaoData:^(BOOL result, NSMutableArray *HongBaoArray) {
                
            }];
        }
    }
    
    
    
    
    
}
@end







//POST /api/v1/redenvelopes/getsendrecordlist





















